package org.example.everyday;

import java.util.Arrays;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * <p>
 *
 * @author anlu
 * @date 2022/5/20 0020 9:52
 */
public class Lc436 {
    public static void main(String[] args) {
        Lc436 lc436 = new Lc436();
        int[][] intervals = new int[][]{{3,4},{2,3},{1,2}};
        int[] res = lc436.findRightInterval(intervals);
        System.out.println(res);

    }
    public int[] findRightInterval(int[][] intervals) {
        int n = intervals.length;
        int[][] startIntervals = new int[n][2];
        for (int i = 0; i < n; i++) {
            startIntervals[i][0] = intervals[i][0];
            startIntervals[i][1] = i;
        }
        Arrays.sort(startIntervals,(o1,o2) -> o1[0] - o2[0]);
        int[] ans = new int[n];

        for (int i = 0; i < n; i++) {
            int left = 0;
            int right = n - 1;
            int target = -1;
            while(left <= right){
                int mid = (left + right) / 2;
                if(startIntervals[mid][0] >= intervals[i][1]){
                    target = startIntervals[mid][1];
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }
            ans[i] = target;
        }
        return ans;

    }
}
